package com.xgp.mywordapp.service.impl;

import com.xgp.mywordapp.mapper.WordsMapper;
import com.xgp.mywordapp.model.Words;
import com.xgp.mywordapp.model.WordsExample;
import com.xgp.mywordapp.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class WordServiceImpl implements WordService {

    @Autowired
    private WordsMapper wordsMapper;

    /**
     * 添加单词
     * @param word
     * @return
     */
    @Override
    public int addWord(Words word) {
        //1,查看是否有该当单词 有则1，无则0
        WordsExample wordsExample = new WordsExample();
        wordsExample.createCriteria().andChineseEqualTo(word.getChinese()).andEnglishEqualTo(word.getEnglish());
        int flag1 = wordsMapper.selectByExample(wordsExample).size();
        if(flag1 != 0) {
            //说明有该单词
            return 2;
        }
        //2，没有该单词则添加
        int flag2 = wordsMapper.insert(word);
        return flag2;
    }

    /**
     * 使单词的错误次数加1
     * @param word
     * @return
     */
    @Override
    public int updateWordAllerror(Words word) {
        WordsExample wordsExample = new WordsExample();
        wordsExample.createCriteria().andChineseEqualTo(word.getChinese()).andEnglishEqualTo(word.getEnglish());
        word = wordsMapper.selectByExample(wordsExample).get(0);
        word.setAllerror(word.getAllerror() + 1);
        int flag = wordsMapper.insertSelective(word);
        return flag;
    }

}
